let app = getApp()
Page({
  scanCode() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        try {
          let data = JSON.parse(res.result)
          wx.showLoading({
            title: '发布奖励中',
          })
          wx.cloud.callFunction({
            name: "db",
            data: {
              $url: "scanCode",
              integral: data.integral
            }
          }).then((res) => {
            let result = res.result
            if (result.code) {
              app.globalData.userInfo.integral += Number(data.integral)
              app.$db.set('userInfo', app.globalData.userInfo)
              wx.hideLoading()
              app.$util.successToShow("获取" + data.integral + "积分！")
            }
          })
        } catch (error) {
          app.$util.errorToShow("扫描到无效验证码")
        }
      }
    })
  }
})